﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVRegister : BaseForm
    {
        public KTVSuiteInfo RoomInfo = null;
        private KTVOrderInfo orderInfo = null;

        public FrmEditKTVRegister()
        {
            InitializeComponent();

            InitDictItems();
        }

        private void InitDictItems()
        {
            this.txtCaculateType.Items.Clear();
            List<KTVCaculteTypeInfo> typeList = BLLFactory<KTVCaculteType>.Instance.GetAll();
            foreach (KTVCaculteTypeInfo typeInfo in typeList)
            {
                this.txtCaculateType.Items.Add(new CListItem(typeInfo.Name, typeInfo.ID.ToString()));
            }
            if (this.txtCaculateType.Items.Count > 0)
            {
                this.txtCaculateType.SelectedIndex = 0;
            }
        }

        private void FrmEditOrder_Load(object sender, EventArgs e)
        {
            if (RoomInfo != null)
            {
                this.lblRoomNo.Text = RoomInfo.RoomNo;
                this.lblRoomType.Text = RoomInfo.RoomType;

                orderInfo = BLLFactory<KTVOrder>.Instance.GetCurrentOrder(RoomInfo.RoomNo);
                if (orderInfo == null)
                    return;

                this.lblAtLeaseMoney.Text = orderInfo.AtLeaseMoney.ToString("C2");
                this.txtBuyOutPrice.Text = orderInfo.BuyOutPrice.ToString("f2");
                this.txtDeposit.Text = orderInfo.DepositMoney.ToString("f2");
                this.txtPrice.Text = orderInfo.Price.ToString("f2");
                this.txtCaculateType.Text = orderInfo.CaculateType;
                this.txtNote.Text = orderInfo.Note;
                this.txtOrderNo.Text = orderInfo.OrderNo;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //改变订单的客户信息即可
                orderInfo.Note = this.txtNote.Text;
                orderInfo.OrderNo = this.txtOrderNo.Text;
                orderInfo.IsCheckOut = false;
                orderInfo.DepositMoney = Convert.ToDecimal(this.txtDeposit.Text);
                orderInfo.CaculateType = this.txtCaculateType.Text;
                orderInfo.Price = Convert.ToDecimal(this.txtPrice.Text);
                orderInfo.BuyOutPrice = Convert.ToDecimal(this.txtBuyOutPrice.Text);

                bool result = BLLFactory<KTVOrder>.Instance.Update(orderInfo, orderInfo.ID.ToString());
                if (result)
                {
                    MessageUtil.ShowTips("保存订单成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageUtil.ShowTips("保存订单失败");
                    return;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError("保存订单出错：" + ex.Message);
            }
        }

        private void txtCaculateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtCaculateType.SelectedItem as CListItem;
            if (item != null && !string.IsNullOrEmpty(item.Value))
            {
                KTVCaculteTypeInfo typeInfo = BLLFactory<KTVCaculteType>.Instance.FindByID(item.Value);
                if (typeInfo != null)
                {
                    this.txtPrice.Text = typeInfo.Price.ToString("f2");
                }
            }
        }
    }
}
